home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
lang_c
/
environ
/
findenv.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-01-01
|
2KB
|
61 lines
/*
program: findenv.c
author: Rick Roberts
date: 16Dec87
findenv uses getenv2 to find the system environment location and
size. It goes to the segment returned by getenv2 and searches for the
string COMSPEC=. If findenv locates the string it prints the
environment's address and size.
Compiled with Microsoft C ver 4.0.
use msc findenv /AL; masm getenv2; link findenv getenv2;
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
extern unsigned int far getenv2(); /* getenv2.asm finds the environment */
int envseg; /* will contain the environment segment */
char far *env; /* far pointer to the system environment */
char far *tem; /* temporary pointer for searching */
int enl; /* will be set to size of environment */
char *search_for(); /* routine to search environment for string */
main()
{
static char comspec[]="COMSPEC=";
enl=getenv2(&envseg); /*get length and address of system environment*/
FP_SEG(env)=envseg; /* set far pointer's segment */
FP_OFF(env)=0; /* set far pointer's offset */
if(search_for(comspec) != NULL) /*see if we can find COMSPEC */
{
printf(" The environment size is %d",enl);
printf(" and its address is %x:0000\n",envseg);
exit(0);
}
printf(" Could not locate COMSPEC variable");
}
char *search_for(str) /*searches environment for string*/
char *str; /* string to search for */
{
int l; /*scratch string length*/
l=strlen(str); /*calculate length of argument string*/
tem=env;
/* &tem the address where tem is stored - remains constant */
/* *tem first letter of each environment variable*/
/* tem offset of first letter of each environment variable*/
while(*tem!=0) /*loop for all strings in environment*/
{
if (strnicmp(tem,str,l)==0) /*if you find it,*/
return(tem); /* return a pointer to it*/
while(*tem++ != 0); /*else, skip to next string*/
}
return(NULL); /*if no such string, return NULL*/
}